//
//  HUDView.swift
//  简易HUD
//
//  Created by emma on 2024/4/17.
//
import Foundation
import UIKit

//MARK: - 字体
func regular(_ size: CGFloat) -> UIFont {
    return UIFont(name: "PingFangSC-Regular", size: size) ?? UIFont.systemFont(ofSize: size)
}
func medium(_ size: CGFloat) -> UIFont {
    return UIFont(name: "PingFangSC-Medium", size: size) ?? UIFont.systemFont(ofSize: size)
}
func bold(_ size: CGFloat) -> UIFont {
    return UIFont(name: "PingFangSC-Semibold", size: size) ?? UIFont.systemFont(ofSize: size)
}

class HUDView: UIView {
    override init(frame: CGRect) {
        super.init(frame: frame)
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

extension UIView {
    func showToast(_ toast: String, duration: Double = 1, isUserInteractionEnabled: Bool = true) {
        self.isUserInteractionEnabled = isUserInteractionEnabled
        let maxTextWidth = UIScreen.main.bounds.width-120
        var textWidth = toast.getWidth(regular(16), 20)
        if textWidth >= maxTextWidth { textWidth = maxTextWidth }
        let textHeight = toast.getHeight(regular(16), textWidth)
        
        let hud = HUDView(frame: CGRect(x: 0, y: 0, width: textWidth+30, height: textHeight+30))
        hud.center = self.center
        hud.layer.cornerRadius = 12
        hud.alpha = 0.0
        hud.backgroundColor = UIColor(red:0, green:0, blue:0, alpha: 0.8)
        
        let label = UILabel(frame: CGRect(x: 15, y: 15, width: textWidth, height: textHeight))
        label.textColor = .white
        label.textAlignment = .left
        label.numberOfLines = 0
        label.text = toast
        hud.addSubview(label)
        
        self.addSubview(hud)
        
        UIView.animate(withDuration: 1, animations: {
            hud.alpha = 1
        })
        
        if duration > 0 {
            DispatchQueue.main.asyncAfter(deadline: .now() + duration) {
                self.isUserInteractionEnabled = true
                UIView.animate(withDuration: 1) {
                    hud.alpha = 0
                } completion: { isCompleted in
                    hud.removeFromSuperview()
                }
            }
        }
    }
    
    func showHUD(isUserInteractionEnabled: Bool = true) {
        self.isUserInteractionEnabled = isUserInteractionEnabled
        let hud = HUDView(frame: CGRect(x: 0, y: 0, width: 78, height: 78))
        hud.center = self.center
        hud.layer.cornerRadius = 12
        hud.alpha = 0.0
        hud.backgroundColor = UIColor(red:0, green:0, blue:0, alpha: 0.8)
        
        let ai = UIActivityIndicatorView(style: .large)
        ai.color = .white
        ai.frame = CGRect(x: 21, y: 21, width: 36, height: 36)
        ai.startAnimating()
        hud.addSubview(ai)
        
        self.addSubview(hud)
        
        UIView.animate(withDuration: 1, animations: {
            hud.alpha = 1
        })
    }
    
    func hideHUD() {
        var huds = [HUDView]()
        for view in self.subviews {
            if view.isKind(of: HUDView.self) {
                huds.append(view as! HUDView)
            }
        }
        for hud in huds {
            UIView.animate(withDuration: 1) {
                hud.alpha = 0
            } completion: { isCompleted in
                hud.removeFromSuperview()
            }
        }
    }
}

extension String {
    //MARK: - 获取宽度、高度
    func getWidth(_ font: UIFont, _ height: CGFloat) -> CGFloat {
        let size = self.boundingRect(with: CGSize(width:900, height: height), options: .usesLineFragmentOrigin, attributes: [.font : font], context: nil).size
        return size.width
    }
    func getHeight(_ font: UIFont, _ width: CGFloat) -> CGFloat {
        let size = self.boundingRect(with: CGSize(width:width, height: CGFloat(MAXFLOAT)), options: .usesLineFragmentOrigin, attributes: [.font : font], context: nil).size
        return size.height
    }
}
